<?php

namespace app\v1\controller;

use app\data\logic\PageLogic;
use app\v1\BaseController;
use think\App;

/**
 * 静态化操作类
 */
class Staticization extends BaseController
{
    protected PageLogic $logic;

    public function __construct(App $app)
    {
        $this->logic = new PageLogic();
        parent::__construct($app);
    }

    /**
     * 创建静态化路由
     */
    public function create(): mixed
    {
        return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
    }

    /**
     * 同步数据
     * @return \think\response\Json
     */
    public function update()
    {
        $domain = env("website.WEBSITE_DOMAIN");
        $adList = $this->logic->getAdList((int)null);
        for ($i = 0; $i < count($adList); $i++) {
            $adList[$i]['des'] = str_replace($domain, '', $adList[$i]['des']);
            $this->logic->updateAd($adList[$i]['ad_id'], ['des' => $adList[$i]['des']], []);
        }

        $pagePartList = $this->logic->getPagePartList((int)null);
        for ($i = 0; $i < count($pagePartList); $i++) {
            $content = str_replace($domain, '', $pagePartList[$i]['content']);
            $this->logic->updatePagePart($pagePartList[$i]['id'], ['content' => $content], []);
        }

        $cmsList = $this->logic->getCmsList((int)null);
        for ($i = 0; $i < count($cmsList); $i++) {
            $content = str_replace($domain, '', $cmsList[$i]['content']);
            $this->logic->updateCms($cmsList[$i]['article_id'], ['content' => $content], []);
        }


        return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
    }

}